﻿using Entities;
using Logger4Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hubberly
{
   public partial class Main
   {
      ////////////////////////////////////////////////////////////////////////
      //
      internal bool initializeListViewGroups()
      {
         try
         {
            if (!new ListView[] { lvProdCities, lvConvoyCities }.All(view =>
               {
                  view.Groups.Add(groupConvoyHub_, groupConvoyHub_);
                  view.Groups.Add(groupInConvoy_, groupInConvoy_);
                  view.Groups.Add(groupNotSupplied_, groupNotSupplied_);
                  return true;
               }))
            {
               throw new Exception("failed to initialize list view groups");
            }
            return true;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      //
      internal bool initializeListViewCities()
      {
         try
         {
            // Fill in all of the defined cities on the various tabs that list them all
            world_ = new World();
            world_.cities.ForEach(city =>
            {
               cbProdConvoy.Items.Add(city);

               ListViewItem item = new ListViewItem(city.name);
               item.Tag = city;
               item.Group = lvProdCities.Groups[groupNotSupplied_];
               lvProdCities.Items.Add(item);

               item = new ListViewItem(city.name);
               item.Tag = city;
               item.Group = lvConvoyCities.Groups[groupNotSupplied_];
               lvConvoyCities.Items.Add(item);
            });
            chkGroupBySupply.Checked = Program.settings.groupByHub;
            return true;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return false;
      }

      ////////////////////////////////////////////////////////////////////////
      //
      internal bool initializeProductionTickets()
      {
         try
         {
            // Initialize the display with empty trade tickets
            TradeTicket ticket;
            List<Material> reverseMaterials = world_.almanac.materials.Reverse<Material>().ToList();
            reverseMaterials.ForEach(material =>
            {
               ticket = new TradeTicket();
               ticket.titleBar = false;
               ticket.world = world_;
               ticket.material = material;
               ticket.Dock = DockStyle.Top;
               ticket.eventFactoriesChanged += new TradeTicket.FactoriesChangedHandler(handleFactoriesChanged);
               ticket.eventSelectAllProducers += new TradeTicket.SelectAllProducersHandler(handleSelectAllProducers);
               ticketContainer.Controls.Add(ticket);
            });

            // The top ticket is the title bar
            ticket = new TradeTicket();
            ticket.titleBar = true;
            ticket.Visible = true;
            ticket.Dock = DockStyle.Top;
            ticketContainer.Controls.Add(ticket);

            // Cache the tickets to save doing this every time, excluding the title ticket
            tradeTickets_ = ticketContainer.Controls.OfType<TradeTicket>().
               Where(thisTicket => thisTicket != ticket).ToList();
            return true;
         }
         catch (System.Exception ex)
         {
            logger_.exception(ex);
         }
         return false;
      }
   }
}
